package com.example.testtouch2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.os.Environment;

import com.demo.androidgame.fx.FileIO;

public class Settings {
    public static boolean soundEnabled = true;
    
    public static int[] highscores = new int[] {120, 90, 70, 70, 30};
    public static int[] highpoints = new int[] { 140, 130, 110, 100, 60};

    public final static String file2 = "test.testtoucher";
  
    public static void load(FileIO files) {
        BufferedReader in = null;
        try {
        	
            in = new BufferedReader(new InputStreamReader(files.readFile(file2)));
            for(int i = 0; i < 5; i++) {
                highscores[i] = Integer.parseInt(in.readLine());   
                highpoints[i] = Integer.parseInt(in.readLine());
            }
        } catch (IOException e) {
            // :( It's ok we have defaults
        } catch (NumberFormatException e) {
            // :/ It's ok, defaults save our day
        } catch (Exception ex) {
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
        }
    }

    public static void save(FileIO files) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(files.writeFile(file2)));
            out.write(Boolean.toString(soundEnabled));
            out.write("\n");
            for(int i = 0; i < 5; i++) {
                out.write(Integer.toString(highscores[i]));
                out.write(Integer.toString(highpoints[i]));
                out.write("\n");
            }
        } catch (IOException e) {
        } catch (Exception ex) {
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }

    public static void addScore(int score) {
        for(int i=0; i < 5; i++) {
            if(highscores[i] < score) {
                for(int j= 4; j > i; j--)
                    highscores[j] = highscores[j-1];
                highscores[i] = score;
                break;
            }
        }
    }
    
    public static void addPoint(int point) {
        for(int i=0; i < 5; i++) {
            if(highpoints[i] < point) {
                for(int j= 4; j > i; j--)
                    highpoints[j] = highpoints[j-1];
                highpoints[i] = point;
                break;
            }
        }
    }

}
